home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 42 / Amiga Format AFCD42 (Issue 126, Aug 1999).iso / -serious- / programming / basic / udpchat / old_stuff / udp_send2.asc < prev    next >
Encoding:
Text File  |  1999-05-14  |  3.1 KB  |  140 lines

  1. ;                      UDP Send code
  2. ;
  3. ;    This code connects to a port like TCP, and then sends
  4. ;  each string with send_() instead of sendto_()
  5. ;
  6. ;  Written by Anton Reinauer <anton@ww.co.nz.
  7. ;
  8. ;  Thanks to Paul Burkey for TCP_Funcs and to Dr. Ercole Spiteri
  9. ;  for TCP-to-Blitz.
  10. ;
  11. ;  Turn overflow errors off if running on a 68000
  12.  
  13.  
  14. WBStartup
  15. NoCli
  16. Hostname.s="localhost"    ; host to connect to
  17. #PORT=3001                ; port to connect to
  18.  
  19. INCLUDE "TCPFuncs.bb"
  20. DEFTYPE .w
  21.  
  22. ;********************************************************
  23.  
  24. .WriteUDP2
  25. Statement WriteUDP2{ad.l,size.w}
  26.   SHARED sock.l
  27.  
  28.   ; This routine writes data via UDP
  29.  
  30.   sockwrite.l=0                         ;Clear Writemask
  31.   sockwrite.l BitSet sock.l             ;set Writemask on our socket
  32.   g=WaitSelect_(2,0,&sockwrite.l,0,0,0) ;Wait until server is ready to read our data
  33.  
  34.   c=send_(sock.l,ad,size,0)  ; send data to connected socket
  35. End Statement
  36.  
  37. .ConnectUDP2
  38. Function  ConnectUDP2{host$,port.w}
  39.   SHARED sock.l,host.sockaddrin,hostlen2.l
  40.   ;
  41.   ; Connect to host at specified port
  42.   ; Return true or False if Connection is made
  43.   ;
  44.  
  45.   sock.l=socket_(2,2,0)      ; open socket
  46.  
  47.   *a.hostent=gethostbyname_(host$)
  48.  
  49.   ;Copy Details to our Sockaddrin structure
  50.  
  51.   bb=CopyMem_(*a.hostent\h_addr_list\ItemA,&host.sockaddrin\sin_addr,*a.hostent\h_length)
  52.  
  53.   host.sockaddrin\sin_port=port    ;Set port number to connect to
  54.   host.sockaddrin\sin_family=2     ;Set type to AT_INET
  55.   hostlen2.l=SizeOf.sockaddrin     ;Get lenght of structure sockaddrin
  56.  
  57.   If connect_(sock.l,host.sockaddrin,hostlen2.l)=-1   ; connect to port on other host like TCP
  58.      CloseSocket_(sock.l)
  59.      NPrint "Not Connected to ",Host$," ",#PORT
  60.      Function Return False
  61.   Else
  62.      NPrint "   Connected to ",Host$," ",#PORT
  63.      Function Return True
  64.   EndIf
  65.  
  66. End Function
  67.  
  68.  
  69. ;****************************************
  70.  
  71. WbToScreen0
  72. Window 0,0,20,300,220,$1|$2|$4|$8|$400,"UDP Send2",1,2
  73.  
  74. WindowOutput0
  75. WindowInput 0
  76.  
  77. If ConnectUDP2 {Hostname,#PORT} =True  ; connect to host
  78.    NPrint "    Type in data, and hit"
  79.    NPrint "      return to send"
  80. Else
  81.   Print "Can't connect to host!"
  82.   Goto Exit:
  83. EndIf
  84.  
  85. ypos=40
  86. xpos=10
  87.  
  88. ;****************************************
  89.  
  90. .Main
  91.  
  92.  Repeat
  93.     Delay_(1)
  94.     b$=Inkey$
  95.     If b$<>""
  96.        If b$=Chr$(13)       ;send string on return key
  97.          WLocate 10,30
  98.          Print "                                       "
  99.          xpos=10
  100.  
  101.          t$="SENT: " + send$
  102.          Gosub Print_String
  103.  
  104.          WriteUDP2{&send$,Len(send$)}    ; send string
  105.          send$=""
  106.        Else                 ; build string to send
  107.          send$=send$+b$
  108.          WLocate xpos,30
  109.          xpos+8
  110.          Print b$          ; echo key press
  111.        EndIf
  112.     EndIf
  113.  
  114.     ev.l=Event
  115.  
  116. Until ev=$200    ; shut down if close gadget hit
  117.  
  118. CloseTCP{}                       ; close connection
  119.  
  120. WLocate 10,30
  121. Print "Connection  closed"
  122.  
  123. Exit:
  124. FreeMem TCPmem.l,$2000
  125. Delay_(10)
  126.  
  127. End
  128.  
  129. ;****************************************
  130.  
  131. .Print_String
  132.       ypos+10
  133.       If ypos=200 Then ypos=80
  134.       WLocate 10,ypos
  135.       Print "                                             "
  136.       WLocate 10,ypos
  137.       Print t$               ; print string received
  138. Return
  139.  
  140.